package com.example.demo.study.designmode.structural.decorator;

public class SugarDecorator implements Coffee{
    private Coffee coffee;
    public SugarDecorator(Coffee coffee)
    {
        System.out.println("咖啡加糖...");
        this.coffee=coffee;
    }

    @Override
    public double getCost() throws Exception{
        if(coffee!=null)
        {
            return coffee.getCost()+2;
        }
        throw new Exception("没有咖啡可加糖!");
    }

    public static void main(String[] args) throws Exception {
        Coffee c=new BasicCoffee();
        SugarDecorator s=new SugarDecorator(c);
        System.out.println("总价是:"+s.getCost());
    }
}
